package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.service.ImanagerService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EmployeeClassificationTrainingSuggestionsMapper;
import com.ruoyi.business.domain.EmployeeClassificationTrainingSuggestions;
import com.ruoyi.business.service.IEmployeeClassificationTrainingSuggestionsService;

import javax.validation.Validator;

/**
 * 人员分类培养建议Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-17
 */
@Service
public class EmployeeClassificationTrainingSuggestionsServiceImpl  extends BaseServiceImpl implements IEmployeeClassificationTrainingSuggestionsService
{
    @Autowired
    private EmployeeClassificationTrainingSuggestionsMapper employeeClassificationTrainingSuggestionsMapper;

    @Autowired
    protected Validator validator;

    @Autowired
    private ImanagerService managerService;

    /**
     * 查询人员分类培养建议
     * 
     * @param id 人员分类培养建议主键
     * @return 人员分类培养建议
     */
    @Override
    public EmployeeClassificationTrainingSuggestions selectEmployeeClassificationTrainingSuggestionsById(Long id)
    {
        return employeeClassificationTrainingSuggestionsMapper.selectEmployeeClassificationTrainingSuggestionsById(id);
    }

    /**
     * 查询人员分类培养建议列表
     * 
     * @param employeeClassificationTrainingSuggestions 人员分类培养建议
     * @return 人员分类培养建议
     */
    @Override
    public List<EmployeeClassificationTrainingSuggestions> selectEmployeeClassificationTrainingSuggestionsList(EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        return employeeClassificationTrainingSuggestionsMapper.selectEmployeeClassificationTrainingSuggestionsList(employeeClassificationTrainingSuggestions);
    }

    /**
     * 新增人员分类培养建议
     * 
     * @param employeeClassificationTrainingSuggestions 人员分类培养建议
     * @return 结果
     */
    @Override
    public int insertEmployeeClassificationTrainingSuggestions(EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        employeeClassificationTrainingSuggestions.setCreateTime(DateUtils.getNowDate());
        int i=  employeeClassificationTrainingSuggestionsMapper.insertEmployeeClassificationTrainingSuggestions(employeeClassificationTrainingSuggestions);

        if(i > 0){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date awd = calendar.getTime();

            managerService.reRun(awd);
        }

        return i;
    }

    /**
     * 修改人员分类培养建议
     * 
     * @param employeeClassificationTrainingSuggestions 人员分类培养建议
     * @return 结果
     */
    @Override
    public int updateEmployeeClassificationTrainingSuggestions(EmployeeClassificationTrainingSuggestions employeeClassificationTrainingSuggestions)
    {
        employeeClassificationTrainingSuggestions.setUpdateTime(DateUtils.getNowDate());
        int rows = employeeClassificationTrainingSuggestionsMapper.updateEmployeeClassificationTrainingSuggestions(employeeClassificationTrainingSuggestions);

        if(rows > 0){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.MONTH, Calendar.JANUARY);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.MILLISECOND, 0);
            Date awd = calendar.getTime();

            managerService.reRun(awd);
        }

        return  rows;
    }

    /**
     * 批量删除人员分类培养建议
     * 
     * @param ids 需要删除的人员分类培养建议主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeClassificationTrainingSuggestionsByIds(Long[] ids)
    {
        return employeeClassificationTrainingSuggestionsMapper.deleteEmployeeClassificationTrainingSuggestionsByIds(ids);
    }

    /**
     * 删除人员分类培养建议信息
     * 
     * @param id 人员分类培养建议主键
     * @return 结果
     */
    @Override
    public int deleteEmployeeClassificationTrainingSuggestionsById(Long id)
    {
        return employeeClassificationTrainingSuggestionsMapper.deleteEmployeeClassificationTrainingSuggestionsById(id);
    }

    @Override
    public String importData(List<EmployeeClassificationTrainingSuggestions> userList, boolean updateSupport, String operName) {
    if (userList == null || userList.isEmpty()) {
        throw new ServiceException("导入员工分类培训建议数据不能为空！");
    }

    int successNum = 0;
    int failureNum = 0;
    StringBuilder successMsg = new StringBuilder();
    StringBuilder failureMsg = new StringBuilder();
        List<String>failureNums = new ArrayList<>();

        int i = 1;
    for (EmployeeClassificationTrainingSuggestions user : userList) {
        try {
            i++;
            if (user.getAwardYear() == null || user.getAwardYear().equals("") ) {
                failureNums.add(String.valueOf(i));
                failureNum++;
                continue ;
            }
            // 验证是否存在这个员工分类培训建议
            EmployeeClassificationTrainingSuggestions existingSuggestion = employeeClassificationTrainingSuggestionsMapper.selectEmployeeClassificationTrainingSuggestionsById(user.getId());
            if (existingSuggestion == null) {
                // 验证数据
                BeanValidators.validateWithException(validator, user);
                // 设置初始值
                user.setCreateTime(DateUtils.getNowDate());
                user.setCreateBy(operName);
                employeeClassificationTrainingSuggestionsMapper.insertEmployeeClassificationTrainingSuggestions(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工分类培训建议 " + " 导入成功");
            } else if (updateSupport) {
                // 验证数据
                BeanValidators.validateWithException(validator, user);
                user.setId(existingSuggestion.getId());
                user.setUpdateTime(DateUtils.getNowDate());
                user.setUpdateBy(operName);
                employeeClassificationTrainingSuggestionsMapper.updateEmployeeClassificationTrainingSuggestions(user);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工分类培训建议 " + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>" + failureNum + "、员工分类培训建议 "  + " 已存在");
            }

        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、员工分类培训建议 " +  " 导入失败：";
            failureMsg.append(msg + e.getMessage());
        }
    }

    if (failureNum > 0) {
        failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
        //throw new ServiceException(failureMsg.toString());
    } else {
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
    }
        setCacheInfo("员工分类培训建议",String.valueOf(successNum),String.valueOf(failureNum),failureNums.stream().collect(Collectors.joining(",")));

    return successMsg.toString() + failureMsg.toString();
}

}
